<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 * 
 * 创建时间：2021-11-12 12:21
 *
 * 项目：rm  -  $  - formValidations.php
 *
 * 作者：liwei 
 */

namespace modules\levfm\table\form\FormData;

use Lev;
use lev\helpers\ScoreHelper;
use modules\levfm\table\form\FormData\validations\cks_settings___maxnum;
use modules\levfm\table\LevfmForms;

!defined('INLEV') && exit('Access Denied LEV');


class formValidations
{
    public static $dosubmit = false;
    public static $formInfo = null;
    public static $accessInputs = null;

    public static function getSettingsKey($inputname) {
        return explode('settings__', $inputname)[1];
    }

    public static function errorMessage($formInfo = null, $dosubmit = false) {
        if (!$formInfo || $formInfo['status']) {
            return Lev::responseMsg(-1, '抱歉，数据表单不存在或已关闭');
        }
        if (!$dosubmit) {
            return false;
        }

        $settings = $formInfo['settings'] = Lev::getSettings($formInfo['settings']);

        static::$dosubmit = $dosubmit;
        static::$formInfo = $formInfo;

        static::$accessInputs = LevfmForms::getAccessInputs();//print_r(static::$accessInputs);
        foreach (static::$accessInputs as $v) {
            if (method_exists(__CLASS__, $method = 'ck_'.$v['inputname'])) {
                $setval = $settings[static::getSettingsKey($v['inputname'])];
                if ($errMsg = static::$method($setval)) {
                    return $errMsg;
                }
            }
        }

        return false;
    }

    public static function ck_settings___check($setval) {
        FormData::$checkData = !$setval;
    }

    public static function ck_settings___maxnum($setval) {
        $setval = Lev::getSettings($setval);
        if (!empty($setval[0]) && !empty(static::$accessInputs['settings___maxnum']['settings']['tablesForm'])) {
            if ($errMsg = cks_settings___maxnum::errorMessage($setval[0], static::$accessInputs['settings___maxnum']['settings']['tablesForm'])) {
                return $errMsg;
            }
        }
    }

    public static function ck_settings___login($setval) {
        if (!$setval && Lev::$app['uid'] <1) {
            return Lev::responseMsg(-5, '抱歉，您需要登陆才可以提交表单');
        }
    }

    public static function ck_settings___payscore($setval) {
        if (static::$dosubmit) {
            if (($setval = json_decode($setval, true)) && $acscore = floatval($setval[0])) {
                $notice = '【'.static::$formInfo['name'].'】发布信息 ';
                if ($acMsg = ScoreHelper::acscoreUse(-$acscore, $notice, $setval[1])) {
                    if ($acMsg['status'] <0) {
                        return $acMsg;
                    }
                    Lev::setNotices($notice.$acMsg['message'], true);
                }
            }
        }
    }

}